//
//  RxEMChatDelegateObservable.swift
//  rxemclient
//
//  Created by yangjian on 2023/4/17.
//

import UIKit

import HyphenateChat
import RxSwift



class RXEMChatObservable: NSObject,EMChatManagerDelegate {
    public let rx_conversationListDidUpdate = PublishSubject<[EMConversation]>();
    public let rx_messagesDidReceive = PublishSubject<[EMChatMessage]>();
    public let rx_cmdMessagesDidReceive = PublishSubject<[EMChatMessage]>();
    public let rx_messagesDidRead = PublishSubject<[EMChatMessage]>();
    public let rx_onConversationRead = PublishSubject<(String,String)>();
    public let rx_messageStatusDidChange = PublishSubject<(EMChatMessage,EMError?)>();

    deinit{
        EMClient.shared().chatManager?.remove(self)
    }
    func messageStatusDidChange(_ aMessage: EMChatMessage, error aError: EMError?) {
        rx_messageStatusDidChange.onNext((aMessage,aError))
    }
    func conversationListDidUpdate(_ aConversationList: [EMConversation]) {
        rx_conversationListDidUpdate.onNext(aConversationList);
    }
    func messagesDidReceive(_ aMessages: [EMChatMessage]) {
        rx_messagesDidReceive.onNext(aMessages);
    }
    func cmdMessagesDidReceive(_ aCmdMessages: [EMChatMessage]) {
        rx_cmdMessagesDidReceive.onNext(aCmdMessages);
    }
    func messagesDidRead(_ aMessages: [EMChatMessage]) {
        rx_messagesDidRead.onNext(aMessages)
    }
    func onConversationRead(_ from: String, to: String) {
        rx_onConversationRead.onNext((from,to))
    }
}
